package com.chenbei.common.handler.introspector;

import com.chenbei.common.annotation.DomainUrl;
import com.chenbei.common.handler.serializer.DomainUrlDeserializer;
import com.chenbei.common.handler.serializer.DomainUrlSerializer;
import com.chenbei.common.properties.AppProperties;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.introspect.Annotated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DomainUrlAnnotationIntrospector extends AnnotationIntrospector {

    @Autowired
    private AppProperties appProperties;

    @Override
    public Version version() {
        return Version.unknownVersion();
    }

    @Override
    public Object findSerializer(Annotated am) {
        if (am.hasAnnotation(DomainUrl.class)) {
            return new DomainUrlSerializer(appProperties.getFileDomain());
        }
        return null;
    }

    @Override
    public Object findDeserializer(Annotated am) {
        if (am.hasAnnotation(DomainUrl.class)) {
            return new DomainUrlDeserializer(appProperties.getFileDomain());
        }
        return null;
    }
}
